package wxdb.planner;

import java.util.*;

import wxdb.parse.*;
import wxdb.parse.absyn.*;
import wxdb.query.*;
import wxdb.ui.Global;

/**
 * The object that executes SQL statements.
 * @author sciore
 */
public class Planner {
   private BasicQueryPlanner qplanner;
   private UpdatePlanner uplanner;
   
   public Planner(BasicQueryPlanner qplanner, UpdatePlanner uplanner) {
      this.qplanner = qplanner;
      this.uplanner = uplanner;
   }
   
   /**
    * Creates a plan for an SQL select statement, using the supplied planner.
    * @param qry the SQL query string
    * @param tx the transaction
    * @return the scan corresponding to the query plan
    */
   public HashMap<String,ArrayList> excuteQueryPlan(QryExp o) {
	   try{
		   Global.queryFields.clear();
		   Global.queryResult.clear();
		   return qplanner.excuteQuery(o);
	   }catch(Exception e){
		   System.out.println(e.getMessage());
	   }
	   return null;
   }
   
   /**
    * Executes an SQL insert, delete, modify, or
    * create statement.
    * The method dispatches to the appropriate method of the
    * supplied update planner,
    * depending on what the parser returns.
    * @param cmd the SQL update string
    * @param tx the transaction
    * @return an integer denoting the number of affected records
    */
   public int executeUpdate(Object obj) {
	   try{
	      if (obj instanceof IstExp)
	         return uplanner.executeInsert((IstExp)obj);
	      else if (obj instanceof DltExp)
	         return uplanner.executeDelete((DltExp)obj);
	      else if (obj instanceof UpdtExp)
	         return uplanner.executeModify((UpdtExp)obj);
	      else if (obj instanceof CrtExp)
	         return uplanner.executeCreateTable((CrtExp)obj);
	      else
	         return 0;
	   }catch(Exception e){
		   e.printStackTrace();
	   }
	   return 0;
   }
}
